﻿///////////////////////////////////////////////////////////////////////////////
//
// This file is part of the BassDrop project
//   https://code.google.com/p/bass-drop/
//
//
// Creator:	Gergely Krajcsovszki (krajcsovszkig@hotmail.com)
// Date:	2014-01-20 13:25
//
//
// Copyright © 2013-2014. Gergely Krajcsovszki
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BassDrop.Core.Common.Data.IO.Portable;

namespace BassDrop.Core.Common.Services
{
	/// <summary>
	/// Facade class for global services provided by the application to its plugins and components
	/// </summary>
	public static class ServiceCenter
	{
		#region Properties

		/// <summary>
		/// File handler object for the application
		/// </summary>
		public static IFileHandler FileHandler
		{
			get;
			private set;
		}

		/// <summary>
		/// Object to be used for encryption and decryption of data.
		/// </summary>
		public static ICryptoProvider CryptoProvider
		{
			get;
			private set;
		}

		/// <summary>
		/// Object to be used for opening and interacting with web sites
		/// </summary>
		public static IWebBrowser WebBrowser
		{
			get;
			private set;
		}

		/// <summary>
		/// Object to be used for showing various UI components to the user from the codebehind
		/// </summary>
		public static IUserInterface UserInterface
		{
			get;
			private set;
		}

		#endregion Properties

		#region Constants

		/// <summary>
		/// Default password used for encryption. As this is publicly visible on the internet,
		/// only use it for not too secure stuff.
		/// </summary>
		private const string DefaultCryptoPassword = "You could break it anyway, so why should I bother making it strong? ...";

		#endregion Constants

		#region Initialization

		/// <summary>
		/// Initializes the service center
		/// </summary>
		/// <param name="fileHandler">the file handler object for the application</param>
		/// <param name="cryptoProvider">object to be used for encryption and decryption of data</param>
		/// <param name="webBrowser">object to be used for opening and interacting with web sites</param>
		/// <param name="userInterface">object to be used for showing various UI components to the user from the codebehind</param>
		internal static void Init( IFileHandler fileHandler, ICryptoProvider cryptoProvider, IWebBrowser webBrowser, IUserInterface userInterface )
		{
			FileHandler = fileHandler;
			CryptoProvider = cryptoProvider;
			WebBrowser = webBrowser;
			UserInterface = userInterface;
		}

		#endregion Initialization

		#region Facade methods for the properties

		/// <summary>
		/// Symmetrically encrypts the passed string with default parameters
		/// and returns a base64 representation of the resulting cyphertext.
		/// <para />
		/// This is not very strong, use the CryptoProvider directly for really secret data.
		/// </summary>
		/// <param name="plainText">the text to encrypt</param>
		/// <returns></returns>
		public static string Encrypt( string plainText )
		{
			return CryptoProvider.Encrypt( plainText, DefaultCryptoPassword );
		}

		/// <summary>
		/// Decrypts the passed cyphertext (as encrypted by the Encrypt method - symmetrically 
		/// with the default parameters and encoded in base64) and returns the resulting plaintext.
		/// </summary>
		/// <param name="cipherText">the encrypted text to decrypt in base64 encoding</param>
		/// <returns></returns>
		public static string Decrypt( string cipherText )
		{
			return CryptoProvider.Decrypt( cipherText, DefaultCryptoPassword );
		}

		/// <summary>
		/// Symmetrically encrypts the passed string using DPAPI and returns a base64 representation of the resulting cyphertext.
		/// This does not need a password, but the encrypted data can only be decrypted later on this computer by this same user.
		/// </summary>
		/// <param name="plainText">the text to encrypt</param>
		/// <returns></returns>
		public static string EncryptLocal( string plainText )
		{
			return CryptoProvider.EncryptLocal( plainText );
		}

		/// <summary>
		/// Decrypts the passed cyphertext (as encrypted by the EncryptLocal method - symmetrically on this computer 
		/// by this same user and encoded in base64)
		/// and returns the resulting plaintext.
		/// </summary>
		/// <param name="cipherText">the encrypted text to decrypt in base64 encoding</param>
		/// <returns></returns>
		public static string DecryptLocal( string cipherText )
		{
			return CryptoProvider.DecryptLocal( cipherText );
		}

		#endregion Facade methods for the properties
	}
}
